var changeEmail = {};
changeEmail.methods = {};

$(function(){

    // 验证身份表单校验
    changeEmail.methods.identityFormCheck();

    // 验证身份表单提交
    changeEmail.methods.identityFormSubmit();

    // 邮箱表单校验
    changeEmail.methods.emailFormCheck();

    // 邮箱表单提交
    changeEmail.methods.emailFormSubmit();

    // 验证身份验证码
    changeEmail.methods.codeBtn($(".shop-identity-form .shop-code-btn"));

    // 邮箱验证码
    changeEmail.methods.codeBtn($(".shop-email-form .shop-code-btn"));

})

// 验证身份表单校验
changeEmail.methods.identityFormCheck = function(){
    //验证规则
    $(".shop-identity-form").validate({

        rules:{
            codeNumber1:{
                required:true,
                minlength:4,
                maxlength:4
            }
        },
        messages:{
            codeNumber1:{
                required:"请输入短信验证码",
                minlength:"验证码为四位",
                maxlength:"验证码为四位"
            }
        }
    });
}

// 验证身份表单提交
changeEmail.methods.identityFormSubmit = function(){
    $(".shop-identity-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-identity-form .shop-identity-btn").on("click",function(){
        if($(".shop-identity-form").valid()){
            $(".shop-account-email-form").hide();
            $(".shop-email-form").show();
            $(".shop-account-email-title .shop-email").addClass("shop-sel");
        }
    })
}

// 邮箱表单校验
changeEmail.methods.emailFormCheck = function(){
    //验证规则
    $(".shop-email-form").validate({
        rules:{
            emailNumber:{
                required:true,
                email : true
            },
            codeNumber2:{
                required:true,
                minlength:4,
                maxlength:4
            }
        },
        messages:{
            emailNumber:{
                required:"请填写邮箱",
                email:"请正确填写邮箱格式" 
            },
            codeNumber2:{
                required:"请输入短信验证码",
                minlength:"验证码为四位",
                maxlength:"验证码为四位"
            }
        }
    });
}

// 邮箱表单提交
changeEmail.methods.emailFormSubmit = function(){
    $(".shop-email-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-email-form .shop-email-btn").on("click",function(){
        if($(".shop-email-form").valid()){
            $(".shop-account-email-form").hide();
            $(".shop-account-complete").show();
            $(".shop-account-email-title .shop-complete").addClass("shop-sel");
        }
    })
}


// 获取验证码公用
changeEmail.methods.codeBtn = function(obj){
    var flag = true;
    obj.on("click", function () {
        // 启动定时器
        var alltime = 60 * 1000;
        if(flag){
            $(this).html("60s");
            flag = false;
            var timer = setInterval(function () {
                alltime = alltime - 1000;
                var str = parseInt(alltime / 1000) + "s";
                obj.html(str.toString());
                if (alltime <= 0) {
                    clearInterval(timer);
                    obj.html("获取验证码");
                    flag = true;
                }
            }, 1000)
        }
        
    });
}